#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "data_proc.h"
#include "camera.h"

int tarNSecond = 60*30;
int tempNSecond = 0;
int g_fd_camera = -1;

void sigroutine(int signo){
    switch (signo){
        case SIGALRM:
            printf("Catch a signal -- SIGALRM \n");
            
            //signal(SIGALRM, sigroutine);   
            tempNSecond ++;
            if(tempNSecond>=tarNSecond)
            {
                //capture one image and send out here.
                printf("get one image here!\n");
                if(g_fd_camera != -1)
                {
                   getOnePicture(g_fd_camera,"image0.jpg"); 
                }
                //encapsulation and send to uart4.
                tempNSecond = 0;
            }
            break;
        case SIGVTALRM:
            printf("Catch a signal -- SIGVTALRM \n");
            signal(SIGVTALRM, sigroutine);
            break;
    }
    return;
}


void startTimer(int msec)
{
    struct itimerval value;          //(1)

    //printf("process id is %d/n", getpid());
    signal(SIGALRM, sigroutine);
    value.it_value.tv_sec = msec/1000;
    value.it_value.tv_usec = (msec%1000) * 1000;
    value.it_interval.tv_sec = msec/1000;
    value.it_interval.tv_usec = (msec%1000) * 1000;
    setitimer(ITIMER_REAL, &value,NULL);    
}

